#!/usr/bin/env just --justfile

set shell := ["bash", "-c"]

# Start test database
start-db: start-docker-db docker-is-ready

# Start test database
[private]
start-docker-db:
    docker run -p 127.0.0.1:5439:5432 -d --name mvtbenchdb --rm sourcepole/mvtbenchdb:v1.2

# Stop test database
stop-db:
    docker stop mvtbenchdb

# Wait for the test database to be ready
[private]
docker-is-ready:
    docker exec mvtbenchdb sh -c 'until pg_isready -h localhost -U postgres; do sleep 1; done'

# -- s3 test environment

start-minio:
    mkdir -p s3data
    docker run --security-opt label=disable -d --rm --name minio -p 9000:9000 -p 9001:9001 -v $PWD/s3data:/data -e MINIO_REGION_NAME=my-region -e MINIO_ROOT_USER=miniostorage -e MINIO_ROOT_PASSWORD=miniostorage minio/minio server /data --console-address ":9001"

stop-minio:
    docker stop minio

setup-minio:
    docker exec minio mc config host add local-docker http://localhost:9000 miniostorage miniostorage
    docker exec minio mc mb local-docker/tiles || true
    docker exec minio mc anonymous set public local-docker/tiles

# Serve mvtbench tile service
bench-server: docker-is-ready
    cargo run --release -- --config={{justfile_directory()}}/bbox-mvtbench.toml serve

# Run HTTP requests benchmark using OHA tool. Use with `just bench-server`
bench-http: (cargo-install "oha")
    @echo "Warming up..."
    oha -z 5s --no-tui http://localhost:8080/xyz/ne_countries/6/33/22.pbf > /dev/null
    oha -z 60s http://localhost:8080/xyz/ne_countries/6/33/22.pbf
    @# oha -z 60s http://localhost:8080/xyz/ne_countries/1/1/0.pbf
    @# oha -z 60s http://localhost:8080/xyz/ne_countries/6/35/20.pbf

# -- mvtbench seeding

# mvtbench seed files
bench-db-seed-files: docker-is-ready
    rm -rf /tmp/mvtbench
    cargo build --release
    ../target/release/bbox-tile-server --config={{justfile_directory()}}/bbox-mvtbench.toml seed --tile-path=/tmp/mvtbench --tileset=ne_countries --maxzoom=6

# mvtbench seed MBTiles
bench-db-seed-mbtiles: docker-is-ready
    rm -f /tmp/mvtbench.mbtiles
    cargo build --release
    ../target/release/bbox-tile-server --config={{justfile_directory()}}/bbox-mvtbench.toml seed --mb-path=/tmp/mvtbench.mbtiles --tileset=ne_countries --maxzoom=6

# mvtbench seed PMTiles
bench-db-seed-pmtiles: docker-is-ready
    rm -f /tmp/mvtbench.pmtiles
    cargo build --release
    ../target/release/bbox-tile-server --config={{justfile_directory()}}/bbox-mvtbench.toml seed --pm-path=/tmp/mvtbench.pmtiles --tileset=ne_countries --maxzoom=6

# Seed files from pmtiles
bench-pm-seed-files:
    rm -rf /tmp/writebench
    cargo build --release
    BBOX_MAPSERVER__NUM_FCGI_PROCESSES=0 ../target/release/bbox-tile-server --config=/dev/null seed --tile-path=/tmp/writebench --tileset=mvtbench --maxzoom=6 ../assets/mvtbench.pmtiles

# Seed mbtiles from pmtiles
bench-pm-seed-mbtiles:
    rm -f /tmp/writebench.mbtiles
    cargo build --release
    BBOX_MAPSERVER__NUM_FCGI_PROCESSES=0 ../target/release/bbox-tile-server --config=/dev/null seed --mb-path=/tmp/writebench.mbtiles --tileset=mvtbench --maxzoom=6 ../assets/mvtbench.pmtiles

# Seed pmtiles from pmtiles
bench-pm-seed-pmtiles:
    rm -f /tmp/writebench.pmtiles
    cargo build --release
    BBOX_MAPSERVER__NUM_FCGI_PROCESSES=0 ../target/release/bbox-tile-server --config=/dev/null seed --pm-path=/tmp/writebench.pmtiles --tileset=mvtbench --maxzoom=6 ../assets/mvtbench.pmtiles

# PG read bench
bench-read-pg: docker-is-ready
    cargo build --release
    ../target/release/bbox-tile-server --config={{justfile_directory()}}/bbox-mvtbench.toml seed --no-store --tileset=ne_countries --maxzoom=6

# MBTiles read bench
bench-read-mbtiles:
    cargo build --release
    BBOX_MAPSERVER__NUM_FCGI_PROCESSES=0 ../target/release/bbox-tile-server --config=/dev/null seed --no-store --tileset=mvtbench --maxzoom=6 /tmp/mvtbench.mbtiles

# PMTiles read bench
bench-read-pmtiles:
    cargo build --release
    BBOX_MAPSERVER__NUM_FCGI_PROCESSES=0 ../target/release/bbox-tile-server --config=/dev/null seed --no-store --tileset=mvtbench --maxzoom=6 ../assets/mvtbench.pmtiles

# Run all seeding benchmarks
bench-seed-all: bench-db-seed-files bench-db-seed-mbtiles bench-db-seed-pmtiles bench-pm-seed-files bench-pm-seed-mbtiles bench-pm-seed-pmtiles bench-read-pg bench-read-mbtiles bench-read-pmtiles

# -- More seeding tests

export AWS_ACCESS_KEY_ID := "miniostorage"
export AWS_SECRET_ACCESS_KEY := "miniostorage"
export S3_ENDPOINT_URL := "http://localhost:9000"

# mvtbench test seed S3
seed-s3: docker-is-ready
    cargo run -- --config={{justfile_directory()}}/bbox-mvtbench.toml --loglevel=info seed --tileset=ne_countries --s3-path=s3://tiles --overwrite=true --maxzoom=3

# -- raster seeding

seed-wms-mbtiles:
    cargo run -- --loglevel=info seed --tileset=gebco --mb-path=/tmp/gebco.mbtile --maxzoom=3

seed-wms-pmtiles:
    cargo run -- --loglevel=info seed --tileset=gebco --pm-path=/tmp/gebco.pmtile --maxzoom=3

seed-map-mbtiles:
    cargo run -- --loglevel=warn seed --tileset=ne_extracts --mb-path=/tmp/ne_extracts.mbtile --maxzoom=3

seed-map-pmtiles:
    cargo run -- --loglevel=warn seed --tileset=ne_extracts --pm-path=/tmp/ne_extracts.pmtile --maxzoom=3

# Check if a certain Cargo command is installed, and install it if needed
[private]
cargo-install $COMMAND $INSTALL_CMD="" *ARGS="":
    #!/usr/bin/env sh
    set -eu
    if ! command -v $COMMAND > /dev/null; then
        if ! command -v cargo-binstall > /dev/null; then
            echo "$COMMAND could not be found. Installing it with    cargo install ${INSTALL_CMD:-$COMMAND} --locked {{ARGS}}"
            cargo install ${INSTALL_CMD:-$COMMAND} --locked {{ARGS}}
        else
            echo "$COMMAND could not be found. Installing it with    cargo binstall ${INSTALL_CMD:-$COMMAND} --locked {{ARGS}}"
            cargo binstall ${INSTALL_CMD:-$COMMAND} --locked {{ARGS}}
        fi
    fi
